﻿using System;
using System.Collections.Generic;

using Styx;
using Styx.Net;
using Styx.Net.Channels;


namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpChannelFactory channelFactory = new TcpChannelFactory();
            channelFactory.ProtocolFactory = new StyxChannelProtocolFactory();
            channelFactory.Url = "127.0.0.1:34000";

            ProxyChannel proxyChannel = new ProxyChannel(channelFactory);

            proxyChannel.Open();

            IStyxNetworkExampleService server = proxyChannel.CreateProxy<IStyxNetworkExampleService>(Services.ExampleService);

            if (server == null)
                throw new Exception("There is no connection to the server");

            server.MessageReceived += new EventHandler<MessageEventArgs>(server_MessageReceived);
            server.UserArrived += new EventHandler<UserInfoEventArgs>(server_UserRegistered);
            server.UserLeft += new EventHandler<UserInfoEventArgs>(server_UserLeft);

            Console.WriteLine(server.ServiceName);

            string cmd = "";

            while(true)
            {
                try
                {
                    Console.Write(">");
                    string command = Console.ReadLine();
                    cmd = command.ToLower();

                    if (cmd == "register")
                    {
                        Console.Write("User name: ");
                        string name = Console.ReadLine();
                        server.RegisterUser(name);
                    }
                    else if (cmd == "unregister")
                    {
                        server.UnregisterUser();
                    }
                    else if (cmd == "message")
                    {
                        Console.Write("To (user name): ");
                        string name = Console.ReadLine();
                        Console.Write("Message: ");
                        string message = Console.ReadLine();
                        server.SendMessage(name, message);
                    }
                    else if (cmd == "list")
                    {
                        List<string> users = server.GetListOfUsers();
                        foreach (string user in users)
                            Console.WriteLine(user);
                    }
                    else if (cmd == "help")
                    {
                        Console.WriteLine();
                        Console.WriteLine("*** Commands ***");
                        Console.WriteLine();
                        Console.WriteLine("register   - registers a new user on server");
                        Console.WriteLine("unregister - removes registered user from server");
                        Console.WriteLine("message    - sends a message to a specified user");
                        Console.WriteLine("list       - prints list of users registeren on the server");
                        Console.WriteLine("help       - prints this help");
                        Console.WriteLine("exit       - closes the application");
                        Console.WriteLine();
                    }
                    else if (cmd == "exit")
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("*** Unknown command ****");
                        Console.WriteLine("use command 'help' to get list of available commands");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0} : {1}", ex.GetType().Name, ex.Message);
                }
            };

            proxyChannel.Close();
            proxyChannel.Dispose();
        }

        static void server_UserLeft(object sender, UserInfoEventArgs e)
        {
            Console.WriteLine("User left : {0}", e.UserName);
        }

        static void server_UserRegistered(object sender, UserInfoEventArgs e)
        {
            Console.WriteLine("User arrived : {0}", e.UserName);
        }

        static void server_MessageReceived(object sender, MessageEventArgs e)
        {
            Console.WriteLine("{0} : {1}", e.UserName, e.Message);
        }
    }
}
